<layout name="layout" />

<div class="pageheader">
	<h1 class="pagetitle">
		<if condition="$showSilu == true">
			<a href="/" class="linkWay">首页/思路优化</a>&nbsp;
			<span class="divider">»</span>&nbsp;
			<a href="/algorithm/condition" class="linkWay">我的思路</a>&nbsp;
			<span class="divider">»</span>&nbsp;
			<a href="#" class="currentWay">执行思路：{$siluInfo.title}</a>
		<elseif condition="$siluAdd == true"/>
			<a href="/" class="linkWay">首页/思路优化</a>
			&nbsp;<span class="divider">»</span>&nbsp;
			<a href="/algorithm/condition_choose" class="linkWay">新增思路</a>
			&nbsp;<span class="divider">»</span>&nbsp;
			<a href="#" class="currentWay">定制思路选择推广组</a>
		<elseif condition="$kaweiAdd == true"/>
			<a href="/" class="linkWay">首页/新增卡位</a>
			&nbsp;<span class="divider">»</span>&nbsp;
			<a href="#" class="currentWay">选择需要卡位的推广组</a>
		<else/>
			<a href="/" class="linkWay">首页</a>
			&nbsp;<span class="divider">»</span>&nbsp;
			<a href="#" class="currentWay">新增卡位</a></if>
	</h1>
</div>
<style>
	.w-200 {
		width: 50%;
	}
	/*.overviewtable_plan tr td, .overviewtable_plan tr th {
		text-align: left !important; 
	}*/
	.center {
		text-align: center !important;
	}
	a.btn:hover {
		text-decoration: none;
		color: #333;
		border: none;
	}
	/*#myThinking option{
		min-width: 30px;
	}*/
	.btn-group-dropdown{
		margin-left: 10px;
		font-weight: bold;
		border: 1px solid #ccc;
		color: #333;
		cursor: pointer;
	}
	.btn3{
		height: 30px !important;margin-left: 12px;
	}
	.dropdown-menu{
		/*max-width: 200px;*/
		widows: 100%;
	}
	.dropdown-menu li{
		margin-top: 10px;
		background-color: #229fdd;
	}
	.dropdown-menu li button{
		margin-left: 10px;
	}
</style>
<div id="contentwrapper" class="contentwrapper nopadding">
	<div class="main">

		<if condition="$showSilu == true">
			<div class="btn-group-dropdown">
				<button class="btn btn3 btn_grey myThinking_li" data-value="{$siluInfo.id}">立即执行</button>
			</div>
		</if>
		<div id="table_datepicker" style="float:right;margin-bottom:10px;"></div>
		<div>
			<table class="metrotable overviewtable_plan" border="0" cellpadding="0" cellspacing="0">
				<colgroup>
					<col class="con1" width="5%">
					<col class="con0" width="5%"><!-- 状态 -->
					<col class="con1" width="15%"><!-- 推广计划名称 -->
					<!-- <col class="con0" width="10%"> --><!-- 计划类型 -->
					<!-- <col class="con0" width="10%"> --><!-- 分时折扣 -->
					<col class="con1" width="5%"><!-- 日限额 -->
					<col class="con0" width="15%"><!-- 投放平台 -->
					<col class="con1" width="7%"><!-- 展现量 -->
					<col class="con0" width="7%"><!-- 点击量 -->
					<col class="con1" width="7%"><!-- 点击率 -->
					<col class="con0" width="7%"><!-- 花费 -->
					<col class="con1" width="7%"><!-- 投入产出比 -->
					<col class="con0" width="8%"><!-- 点击转化率 -->
					<col class="con1" width="12%"><!-- 操作 -->
				</colgroup>
				<thead>
				<tr>
					<th class="head1 nolborder">
						<a id="selectAll" class="btn btn3 btn_unselectall btn_selectall tooltips" href="javascript:void(0);" title="全选">全选</a>
					</th>
					<th class="head0"><strong>状态</strong></th>
					<th class="head1"><strong>推广计划名称</strong></th>
					<!-- <th class="head1">计划类型</th> -->
					<!-- <th class="head0">分时折扣</th> -->
					<th class="head1"><strong>日限额</strong></th>
					<th class="head0"><strong>投放平台</strong></th>
					<th class="head1"><strong>展现量</strong></th>
					<th class="head0"><strong>点击量</strong></th>
					<th class="head1"><strong>点击率</strong></th>
					<th class="head0"><strong>花费</strong></th>
					<th class="head1"><strong>投入产出比</strong></th>
					<th class="head0"><strong>点击转化率</strong></th>
					<th class="head1"><strong>操作</strong></th>
				</tr>
				</thead>
				<tbody id="campaign_report">

				</tbody>
			</table>
		</div>
		<if condition="$siluAdd == true">
			<button class="stdbtn add-silu" style="padding:8px 30px;margin-left:565px;background:#229fdd;color:#fff;" data-type="custom">下一步</button>
		</if>
	</div>
</div>

<script id="adgroup_box" type="text/html" >
	<div class="widget adjustpricewidget" id="adgroups_box">
		<div style="max-width:1366px; max-height:400px; overflow-y:scroll;">
			<table cellpadding="0" cellspacing="0" border="0" class="metrotable">
				<colgroup>
					<col class="con1" width="5%">
					<col class="con0" width="30%">
					<col class="con1" width="5%">
					<col class="con1" width="5%">
					<col class="con1" width="5%">
					<col class="con1" width="5%">
					<col class="con1" width="10%">
					<col class="con1" width="10%">
					<col class="con1" width="5%">
					<col class="con1" width="5%">
					<col class="con1" width="15%">
				</colgroup>
				<thead>
				<tr>
					<th></th>
					<th class="head0 nolborder" style="width:25%;" ><a href="javascript:void(0);">宝贝标题</a></th>
					<th class="head1 sortable sorting_desc" data-sort='status'><a href="javascript:void(0);">状态</a></th>
					<th class="head1 sortable" data-sort='impressions'><a href="javascript:void(0);" >展现量</a></th>
					<th class="head0 sortable" data-sort='click'><a href="javascript:void(0);" >点击量</a></th>
					<th class="head1 sortable" data-sort='ctr'><a href="javascript:void(0);" >点击率</a></th>
					<th class="head1 sortable" data-sort='cpc'><a href="javascript:void(0);" >单次点击花费</a></th>
					<th class="head1 sortable" data-sort='cpm'><a href="javascript:void(0);" >千次展现花费</a></th>
					<th class="head1 sortable" data-sort='cost'><a href="javascript:void(0);" >花费</a></th>
					<th class="head0 sortable" data-sort='sales'><a href="javascript:void(0);" >销售额</a></th>
					<th class="head1 sortable" data-sort='roi'><a href="javascript:void(0);" >投入产出比</a></th>
				</tr>
				</thead>
				<tbody id="adgroups_box_body">
				<% for(var i = 0; i < data.length; i ++) { %>
				<% if (data[i].online_status == 'online') { %>
				<tr data-adgroup_id="<%= data[i].adgroup_id %>">
					<td>
						<input type="checkbox" style="margin-left:13px;" class="adgroup_select_items" data-adgroup_id="<%= data[i].adgroup_id %>">
					</td>
					<td>
						<div class="imgtxt60">
							<div class="photo">
								<a class="img60" href="javascript:;">
									<img src="<%= data[i].img_url %>" alt="<%= data[i].title %>">
								</a>
							</div>
							<p class="title">
								<a href="javascript:;"><%= data[i].title %></a>
							</p>
						</div>
					</td>
					<td class="green"><strong>推广中</strong></td>
					<td><%= data[i].summary.impressions %></td>
					<td><%= data[i].summary.click %></td>
					<td><%= data[i].summary.ctr %></td>
					<td><%= data[i].summary.cpc %></td>
					<td><%= data[i].summary.cpm %></td>
					<td><%= data[i].summary.cost %></td>
					<td><%= data[i].summary.sales %></td>
					<td><%= data[i].summary.roi %></td>
				</tr>
				<% } %>
				<%}%>
				</tbody>
			</table>
		</div>
	</div>
</script>
<script id="campaign_pre_report" type="text/html" >
	<% for(var i = 0; i < data.length; i ++) { %>
	<% if (data[i].online_status == 'online') { %>
	<tr>
		<td>
			<input type="checkbox" style="margin-left:13px;" class="select_items" data-campaign_id="<%= data[i].campaign_id %>">
		</td>
		<td class="green"><strong>推广中</strong></td>
		<td><%= data[i].title %></td>
		<td><%= data[i].budget %>元</td>
		<td>计算机 移动设备</td>
		<td><%= data[i].report.impressions %></td>
		<td><%= data[i].report.click %></td>
		<td><%= data[i].report.ctr %>%</td>
		<td>￥<%= data[i].report.cost %></td>
		<td><%= data[i].report.roi %></td>
		<td><%= data[i].report.cvr %></td>
		<td>
			<a href="javascript:;" class="adgroups_select" data-campaign_id="<%= data[i].campaign_id %>">选择推广组</a>
			</br><span class="adgroup_num_name">已选择<span style="color:red" class="adgroup_num">0</span>款宝贝</span>
		</td>
	</tr>
	<% } %>
	<%}%>
</script>
<script>


	var checkMyThinking = {
		status : false,
		value : 0
	};
	from = wt.formatDate(-1);
	to = wt.formatDate(-1);
	_campaign_iids = {};
	_adgroup_iids = {};
	_checkAll = false;
	_params = {
		from: from,
		to: to,
		format: 'json',
		sort: 'status',
		asc: 'desc'
	};
	_adgroup_select_num = 0;
	function getDataCampaigns() {
		// _charter = $('#chartplace').highcharts();
		//set loading of all fields
		// if(_charter){
		// 	_charter.showLoading('正在读取数据中，请稍后');
		// }

		var params = { from: from , to : to };

		wt.post('/report/getcampaigns', params, function(r) {
			var o = $.parseJSON(r);
			if( o.status == 200 ){
				var tpl = template.render('campaign_pre_report', o);
				$('#campaign_report').html(tpl);
				_campaign_iids = {};
				_adgroup_iids = {};
			}else{
				//TODO , error when ajax
			}
		});
	}
	function run () {
		var urlString = '',
				urlString_adgroup = '';
		if(count(_campaign_iids) == 0 && count(_adgroup_iids) == 0) {
			checkMyThinking.status = false;
			checkMyThinking.value = 0;
			$('#myThinking').val('');
			wt.showBox('至少选择一个推广计划或推广组','警告');
			return ;
		}
		$.each(_campaign_iids, function(key, campaign_id){
			urlString+=campaign_id+',';
		});
		urlString = encodeURIComponent(urlString.substring(0,urlString.length-1));

		$.each(_adgroup_iids, function(key, adgroup_id){
			urlString_adgroup+=adgroup_id+',';
		});
		urlString_adgroup = encodeURIComponent(urlString_adgroup.substring(0,urlString_adgroup.length-1));
		var thinkOption = (checkMyThinking.status == true) ? 'type=myThinking&myThinking=' + checkMyThinking.value + '&' : '';
		var url = '/shortcut/keywordsbatch?' + thinkOption + 'campaign_id='+urlString+ (urlString_adgroup ? '&adgroup_id='+urlString_adgroup : '');
		window.location.href=url;
	}
	function init_sort (obj) {
		obj.on('click','.sortable',function() {
			$sort_obj = $(this);
			_params.sort = $(this).data('sort');
			if($sort_obj.hasClass('sorting_desc')) {
				obj.find('.sortable').removeClass('sorting_desc');
				obj.find('.sortable').removeClass('sorting_asc');
				$sort_obj.removeClass('sorting_desc');
				$sort_obj.addClass('sorting_asc');
				_params.asc = 'asc';
			} else {
				obj.find('.sortable').removeClass('sorting_desc');
				obj.find('.sortable').removeClass('sorting_asc');
				$sort_obj.removeClass('sorting_asc');
				$sort_obj.addClass('sorting_desc');
				_params.asc = 'desc';
			}
			wt.get( '/campaign/adgroups' , _params , function(r){
				wt.overlay('show');
				var o = $.parseJSON(r);
				var adgroups_html = template.render('adgroup_box', o);
				// alert($(adgroups_html).html());
				// $adgroups_box = $(document).find("#adgroups_box");
				$adgroups_box.find('#adgroups_box_body').html($(adgroups_html).find('#adgroups_box_body').html());
				init_select();
			});
		})
	}
	function init_select () {
		$j = $(document).find('#adgroups_box_body');
		$j.find('.adgroup_select_items').attr('checked', true);
		$j.find('.adgroup_select_items').attr('checked', false);
		$j.find('tr').each(function(i,e) {
			if( _adgroup_iids.hasOwnProperty('k_'+$(e).data('adgroup_id')) ) {
				$(e).find('.adgroup_select_items').attr('checked', true);
			}
			// 	// alert($(e).data('adgroup_id'));
		});
	}

	$(document).ready(function(){
		if(!window.siluHelper){
			siluHelper = $.siluHelper(".add-silu", {});
		}

		var $adgroups_box = $(document).find('#adgroups_box');
		/** datepicker **/
		var _datepicker = new wt.datepicker("#table_datepicker" , {
			startDate:  Date.today().add({ days: -1 }).toString('yyyy-MM-dd'),
			onChange : function(start, end){
				from = start;
				to = end;
				getDataCampaigns();
			}
		});
		getDataCampaigns();
		// 全选
		$('#selectAll').click(function() {
			$(this).toggleClass('btn_unselectall');
			_checkAll = _checkAll ? false : true;
			if(_checkAll) {
				$('.select_items').attr('checked', true);
			} else {
				$('.select_items').attr('checked', false);
			}
			$('.select_items').each(function(i,e) {
				if($(e).attr('disabled') == 'disabled') {
					$(e).attr('checked', false);
				} else if(_checkAll) {
					_campaign_iids['k_'+$(e).data('campaign_id')] = $(e).data('campaign_id');
				} else {
					_campaign_iids = {};
				}
			});
		});

		// 多选
		$(document).on('click','.select_items',function() {
			var campaign_id = $(this).data('campaign_id');
			var j = 0,
					s = $('.select_items').length;
			var $this = $(this);
			if( _campaign_iids.hasOwnProperty('k_'+campaign_id) ){
				$this.attr("checked" , false);
				delete _campaign_iids['k_'+campaign_id];
			} else {
				_campaign_iids['k_'+campaign_id] = campaign_id;
				$this.attr("checked" , true);
			}
			$('.select_items').each(function(i,e) {
				if($(e).attr('checked')) {
					j++;
				}
			})
			if(j < s) {
				_checkAll = false;
				$('#selectAll').addClass('btn_unselectall');
			} else {
				$('#selectAll').removeClass('btn_unselectall');
			}
		});

		// 推广组多选
		$(document).on('click','.adgroup_select_items',function() {
			var adgroup_id = $(this).data('adgroup_id');
			var j = 0;
			var $this = $(this);
			if( _adgroup_iids.hasOwnProperty('k_'+adgroup_id) ){
				$this.attr("checked" , false);
				delete _adgroup_iids['k_'+adgroup_id];
			} else {
				_adgroup_iids['k_'+adgroup_id] = adgroup_id;
				$this.attr("checked" , true);
			}
			$('.adgroup_select_items').each(function(i,e) {
				if($(e).attr('checked')) {
					j++;
				}
			})
			var campaign_id = $('#adgroups_box_campaign_id').val();
			var $p_this = $(document).find('input[data-campaign_id="'+campaign_id+'"]');
			if(j > 0) {
				$p_this.attr('disabled',true);
			} else {
				$p_this.attr('disabled',false);
			}
		})

		// top btn 批量优化
		$('.keywordsbatch').click(function() {
			run();
		});

		$(document).on('click', '.adgroups_select', function() {
			var $this = $(this);
			var campaign_id = $this.data('campaign_id');
			if( _campaign_iids.hasOwnProperty('k_'+campaign_id) ){
				wt.showBox('已经全选该推广计划，不能再进行选择推广组','警告');
				return ;
			}
			wt.showNotice('正在读取数据中，请稍后……');
			_params.campaign_id = campaign_id;
			_params.from = from;
			_params.to = to;
			wt.get('/campaign/adgroups', _params, function(r) {
				var o = $.parseJSON(r);
				if(o.status == 200) {
					var adgroups_html = template.render('adgroup_box', o);
					wt.showBox(adgroups_html, '选择推广组', function(t) {
						if(t) {
							_adgroup_select_num = 0;
							$('.adgroup_select_items').each(function(i,e) {
								if($(e).attr('checked')) {
									_adgroup_select_num++;
								}
							})
							$this.parent('td').find('.adgroup_num').text(_adgroup_select_num);
							$this.parent('td').find('.adgroup_num_name').css({color:'red'});
						}/* else {
						 $(document).find('.adgroup_select_items').each(function(i,e) {
						 if($(e).attr('checked')) {
						 $(e).trigger('click');
						 }
						 });
						 }*/
					});
					$adgroups_box = $(document).find("#adgroups_box");
					$adgroups_box.after('<input type="hidden" value="'+campaign_id+'" id="adgroups_box_campaign_id">');
					init_select();
					init_sort($adgroups_box);

				} else {

				}
			});
		});

		$('.add-silu').click(function() {
			var p = {
				campaign_id : '',
				adgroup_id : '',
				siluType : 'public',
				edit: false
			};
			p.siluType = $(this).data('type');
			if(p.siluType == 'custom' && count(_campaign_iids) > 0) {
				$.each(_campaign_iids, function(key, campaign_id){
					p.campaign_id+=campaign_id+',';
				});
				p.campaign_id = (p.campaign_id).substring(0,(p.campaign_id).length-1)
			}
			if(p.siluType == 'custom' && count(_adgroup_iids) > 0) {
				$.each(_adgroup_iids, function(key, adgroup_id){
					p.adgroup_id+=adgroup_id+',';
				});
				p.adgroup_id = p.adgroup_id.substring(0,p.adgroup_id.length-1)
			}

			siluHelper.setParams(p);
			siluHelper.init();
		});

		$('.myThinking_li').click(function(argument) {
			var $this = $(this),
					cus_campaign_id = "",
					cus_adgroup_id = "";
			$.each(_campaign_iids, function(key, campaign_id){
				cus_campaign_id += campaign_id + ',';
			});
			$.each(_adgroup_iids, function(key, adgroup_id){
				cus_adgroup_id += adgroup_id + ',';
			});
			siluHelper.execute({
				id: $this.data('value'),
				campaign_id:cus_campaign_id.substring(0,cus_campaign_id.length-1),
				adgroup_id:cus_adgroup_id.substring(0,cus_adgroup_id.length-1)
			});
		});

		$('.think-edit').click(function(){
			var p = {
				edit : true,
				silu_id : $(this).data('id')
			};
			siluHelper.setParams(p);
			siluHelper.init();
		});

		$('.run-kawei').click(function() {
			var urlString = '',
					urlString_adgroup = '';
			if(count(_campaign_iids) == 0 && count(_adgroup_iids) == 0) {
				wt.showBox('至少选择一个推广计划或推广组','警告');
				return ;
			}
			$.each(_campaign_iids, function(key, campaign_id){
				urlString+=campaign_id+',';
			});
			urlString = encodeURIComponent(urlString.substring(0,urlString.length-1));

			$.each(_adgroup_iids, function(key, adgroup_id){
				urlString_adgroup+=adgroup_id+',';
			});
			urlString_adgroup = encodeURIComponent(urlString_adgroup.substring(0,urlString_adgroup.length-1));
			var url = '/shortcut/keywordsbatch?campaign_id='+urlString+ (urlString_adgroup ? '&adgroup_id='+urlString_adgroup : '');
			$.cookie('kaweiSetting', $.encodeJSON(url), { path : "/" });
			msg.load();
			setTimeout(function(){
				msg.closeAll('loading');
				window.location.href = url;
			}, 500);
		});
	});
</script>